package com.sunshine.wrongquestionset.activity.main

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.dialog.MaterialAlertDialogBuilder

import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.activity.exercise_history.ExerciseHistoryActivity
import com.sunshine.wrongquestionset.activity.exercise_history.ExerciseHistoryViewModel
import com.sunshine.wrongquestionset.activity.me.MeActivity
import com.sunshine.wrongquestionset.activity.my_subject.MySubjectActivity
import com.sunshine.wrongquestionset.activity.question.QuestionActivity
import com.sunshine.wrongquestionset.activity.question_set.QuestionSetActivity
import com.sunshine.wrongquestionset.callback.OnRefreshListener
import com.sunshine.wrongquestionset.room.entity.AppConfigEntity
import com.sunshine.wrongquestionset.utils.TagUtils
import kotlinx.android.synthetic.main.fragment_me.*

class MeFragment : Fragment(), View.OnClickListener {

    private lateinit var viewModel: MainViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_me, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(requireActivity().application)).get(MainViewModel::class.java)

        (requireActivity() as MainActivity).getLoginUserAppConfig(object : OnRefreshListener<AppConfigEntity>() {
            override fun onUpdate(obj: AppConfigEntity) {
                super.onUpdate(obj)

                viewModel.getUserById(obj.id).observe(viewLifecycleOwner, Observer {
                    textView_userId.text = it?.nickname
                })
            }
        })

        button_my_question_set.setOnClickListener(this)
        button_subject_name.setOnClickListener(this)
        button_my_question.setOnClickListener(this)
        button_exercise_history.setOnClickListener(this)
        materialCardView_user_info.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.button_my_question_set -> {
                startActivity(Intent(requireActivity(), QuestionSetActivity::class.java))
            }
            R.id.button_subject_name -> {
                startActivity(Intent(requireActivity(), MySubjectActivity::class.java))
            }
            R.id.button_my_question -> {
                val intent = Intent(requireActivity(), QuestionActivity::class.java)
                intent.putExtra("type", QuestionActivity.TYPE_ALL)
                startActivity(intent)
            }
            R.id.button_exercise_history -> {
                startActivity(Intent(requireActivity(), ExerciseHistoryActivity::class.java))
            }
            R.id.materialCardView_user_info -> {
                startActivityForResult(Intent(requireActivity(), MeActivity::class.java), TagUtils.ME_FRAGMENT)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == TagUtils.ME_FRAGMENT && resultCode == TagUtils.ME_ACTIVITY) {
            //刷新昵称
            (requireActivity() as MainActivity).getLoginUserAppConfig(object : OnRefreshListener<AppConfigEntity>() {
                override fun onUpdate(obj: AppConfigEntity) {
                    super.onUpdate(obj)

                    viewModel.getUserById(obj.id).observe(viewLifecycleOwner, Observer {
                        textView_userId.text = it?.nickname
                    })
                }
            })
        }
    }
}